$(function () {
    $("#jqGrid").Grid({
        url: '../product/car/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '品牌', name: 'carSerie', index: 'car_serie', width: 80},
			{label: '代码', name: 'code', index: 'code', width: 80},
			{label: '车型', name: 'carModel', index: 'car_model', width: 80},
			{label: '款式', name: 'design', index: 'design', width: 80},
			{label: '配置', name: 'config', index: 'config', width: 80},
			{label: '外观', name: 'colour', index: 'colour', width: 80},
			{label: '排量', name: 'displacement', index: 'displacement', width: 80},
			{label: '变速箱', name: 'speedBox', index: 'speedBox', width: 80},
			{label: '动力', name: 'power', index: 'power', width: 80}]
    });
});
var setting = {
    data: {
        simpleData: {enable: true,idKey:"id",pIdKey:"parentId",rootPId: -1},
        key:{url: "nourl"}
    }
};
var ztree;
var vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        showDetail: false,
        showDiscount: false,
        showRoyalty: false,
        title: null,
        modalImport:false,
		productCar: {classifyId:"boq01",classifyName:"车辆"},
		productDiscount: {},
		productRoyalty: {},
		ruleValidate: {
			carModel: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
			carModel: '',
			carSerie: '',
			design: '',
			config: '',
			colour:'',
			displacement: '',
			speedBox: '',
			power:'',
			code:''
		}
	},
	methods: {
		showImport:function(){
			vm.modalImport=true;
			$('.ivu-upload-list li').remove();
		},
		reloadImport:function(){
			vm.modalImport=false;
		},
		downFile: function () {
            //下载模板
            window.location.href = "../statics/model/carModel.xlsx";
        },
        impsuccess: function (res, file) {
            alert('导入成功', function (index) {
                vm.reload();
                vm.modal = false;
            });
        },
        imperror: function () {
            alert('导入失败，请下载模板并根据模板数据进行导入', function (index) {
                vm.reload();
            });
        },
		productClassifyTree: function () {
            openWindow({
                title: "选择分类",
                area: ['300px', '450px'],
                content: jQuery("#productClassifyLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级菜单
                    vm.productCar.classifyId = node[0].id;
                    vm.productCar.classifyName = node[0].name;
                    layer.close(index);
                }
            });
        },
		getProductClassifyTree:function(){
			Ajax.request({
                url: "../product/classify/queryAll?allParentId=car01",
                async: true,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#productClassifyTree"), setting, r.list);
                    var node = ztree.getNodeByParam("id", vm.productCar.classifyId);
                    if (node) {
                        ztree.selectNode(node);
                        vm.productCar.classifyName = node.name;
                    } else {
                        node = ztree.getNodeByParam("id", "car01");
                        ztree.selectNode(node);
                        vm.productCar.classifyName = node.name;
                    }
                }
            });
		},
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.showDetail=true;
			vm.showDiscount=false;
			vm.showRoyalty=false;
			vm.title = "新增";
			vm.productCar = {classifyId:"boq01",classifyName:"车辆"};
			vm.getProductClassifyTree();
		},
		update: function (event) {
            var id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
			vm.showDetail=true;
			vm.showDiscount=false;
			vm.showRoyalty=false;
            vm.title = "修改";
            vm.getInfo(id);
		},
		discountset: function (event) {
			var id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
			vm.showDetail=false;
			vm.showDiscount=true;
			vm.showRoyalty=false;
			vm.title = "返利设置";
			vm.productDiscount.productId=id;
			vm.getProductDiscountInfo(id);
		},
		royaltyset: function (event) {
			var id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
			vm.showDetail=false;
			vm.showDiscount=false;
			vm.showRoyalty=true;
			vm.title = "提成设置";
			vm.productRoyalty.productId=id;
			vm.getProductRoyaltyInfo(id);
		},
		saveOrUpdate: function (event) {
            var url = vm.productCar.id == null ? "../product/car/save" : "../product/car/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.productCar),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		saveDiscount: function (event) {
			var url = "../base/productdiscount/save";
			Ajax.request({
				url: url,
				params: JSON.stringify(vm.productDiscount),
				type: "POST",
				contentType: "application/json",
				successCallback: function (r) {
					alert('操作成功', function (index) {
						vm.reload();
					});
				}
			});
		},
		saveRoyalty: function (event) {
			var url = "../base/saleroyalty/save";
			Ajax.request({
				url: url,
				params: JSON.stringify(vm.productRoyalty),
				type: "POST",
				contentType: "application/json",
				successCallback: function (r) {
					alert('操作成功', function (index) {
						vm.reload();
					});
				}
			});
		},
		del: function (event) {
            var ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../product/base/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../product/car/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.productCar = r.productCar;
                }
            });
		},
		getProductDiscountInfo: function(id){
			Ajax.request({
				url: "../base/productdiscount/info/"+id,
				async: true,
				successCallback: function (r) {
					if(r.productDiscount){
						vm.productDiscount = r.productDiscount;
					}
				}
			});
		},
		getProductRoyaltyInfo: function(id){
			Ajax.request({
				url: "../base/saleroyalty/info/"+id,
				async: true,
				successCallback: function (r) {
					if(r.productRoyalty){
						vm.productRoyalty = r.productRoyalty;
					}
				}
			});
		},
		reload: function (event) {
			vm.showList = true;
			vm.showDetail=false;
			vm.showDiscount=false;
			vm.showRoyalty=false;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {
					'carModel': vm.q.carModel,
					'carSerie': vm.q.carSerie,
					'design': vm.q.design,
					'config': vm.q.config,
					'colour':vm.q.colour,
					'displacement': vm.q.displacement,
					'speedBox': vm.q.speedBox,
					'power':vm.q.power,
					'code':vm.q.code
				},
                page: 1
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
				carModel: '',
				carSerie: '',
				design: '',
				config: '',
				colour:'',
				displacement: '',
				speedBox: '',
				power:'',
				code:''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleDiscountSubmit: function (name) {
        	handleSubmitValidate(this, name, function () {
        		vm.saveDiscount()
        	});
        },
        handleRoyaltySubmit: function (name) {
        	handleSubmitValidate(this, name, function () {
        		vm.saveRoyalty()
        	});
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});